package cn.lili.controller.user.redpack;

import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.member.entity.dos.RedPackTask;
import cn.lili.modules.member.entity.dos.RedPackUser;
import cn.lili.modules.member.service.RedPackTaskService;
import cn.lili.modules.member.service.RedPackUserHistoryService;
import cn.lili.modules.member.service.RedPackUserService;
import cn.lili.modules.member.service.RedPackUserTaskService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@Api(tags = "红包广场任务接口")
@RequestMapping("/user/redPack")
@RequiredArgsConstructor
public class RedPackTaskController {

    private final RedPackTaskService redPackTaskService;

    private final RedPackUserService redPackUserService;

    private final RedPackUserHistoryService redPackUserHistoryService;

    private final RedPackUserTaskService redPackUserTaskService;

    @ApiOperation(value = "分页获取红包任务数据")
    @GetMapping
    public ResultMessage<Page<RedPackTask>> getByPage(RedPackTask redPackTask, PageVO page) {
        return ResultUtil.data(redPackTaskService.getByPage(redPackTask, page));
    }

    @ApiOperation(value = "获取红包任务列表")
    @GetMapping("/list")
    public ResultMessage<List<RedPackTask>> getByList() {
        return ResultUtil.data(redPackTaskService.getByList());
    }

    @ApiOperation(value = "发布任务")
    @PostMapping("/add")
    public ResultMessage<Void> add(@RequestBody RedPackTask redPackTask) {
        redPackTaskService.addRedPackTask(redPackTask);
        return ResultUtil.success();
    }

    @ApiOperation(value = "编辑任务")
    @PostMapping("/edit")
    public ResultMessage<Void> edit(@RequestBody RedPackTask redPackTask) {
        redPackTaskService.updateRedPackTask(redPackTask);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除任务")
    @GetMapping("/delete/{id}")
    public ResultMessage<Void> edit(@PathVariable String id) {
        redPackTaskService.deleteRedPackTask(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "关闭任务")
    @GetMapping("/close/{id}")
    public ResultMessage<Void> close(@PathVariable String id) {
        redPackTaskService.closeTask(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "获取用户红包数据")
    @GetMapping("/getUserRedPack")
    public ResultMessage<RedPackUser> getUserRedPack(String sessionId) throws ParseException {
        return ResultUtil.data(redPackUserService.getUserInfo(sessionId));
    }


    @ApiOperation(value = "分页获取用户红包变更数据")
    @GetMapping("/getRecordsByPage")
    public ResultMessage<Object> getRecordsByPage (PageVO page) {
        return ResultUtil.data(redPackUserHistoryService.getByPage(page));
    }

    @ApiOperation(value = "获取广告商品")
    @GetMapping("/getAdvert")
    public ResultMessage<String> getAdvertGoods (String taskId) {
        return ResultUtil.data(redPackTaskService.getAdvertGoods(taskId));
    }

    @ApiOperation(value = "广告任务回调")
    @GetMapping("/advertCallback")
    public ResultMessage<Void> advertCallback (String taskId) {
        redPackUserTaskService.advertCallback(taskId);
        return ResultUtil.success();
    }

    @ApiOperation(value = "开红包")
    @GetMapping("/openRedPack")
    public ResultMessage<Object> openRedPack (String taskId) {
        return ResultUtil.data(redPackUserTaskService.openRedPack(taskId));
    }
}